#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"

int
main(int argc, char *argv[])
{
  int fds[2],fds2[2];

  if(pipe(fds) != 0){
    printf("%d: pipe() failed\n",getpid());
    exit(1);
  }

  if(pipe(fds2) != 0){
    printf("%d: pipe() failed\n",getpid());
    exit(1);
  }
  int pid = fork();
  if(pid == 0){
    //child
    close(fds[1]);
    close(fds2[0]);
    char buf[1];
    read(fds[0],buf,1);
    printf("%d: received ping\n",getpid());
    write(fds2[1],"pong",4);
    close(fds[0]);
  } else if(pid > 0){
    //parent
    close(fds[0]);
    close(fds2[1]);
    write(fds[1],"ping",4);
    char buf[1];
    read(fds2[0],buf,1);
    printf("%d: received pong\n",getpid());
    close(fds[1]);
    close(fds2[0]);
  }else {
    printf("%d: fork() failed\n",getpid());
    exit(1);
  }
  

  exit(0);
}
